﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LdExpress;
using System.Data;
using MySql.Data.MySqlClient;
using System.Globalization;

public partial class CreateCusdoc : System.Web.UI.Page
{
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public static string strGroupContType = "";
    public double spdGW_Sum = 0;
    public double spdMEAS_Sum = 0;
    public double spdVOL_Sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        DataTable dtList;
        int nNumOfPages = 0;
        if (Session["logined_id"] != null)
        {
            if (Session["cusdoc"] != "") nPermissions = int.Parse(Session["cusdoc"].ToString());

        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            //Get MBLSHPR
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                long lcusdocID = long.Parse(Request.QueryString["ID"].ToString());
                strGroupContType = ShippingDetail.GetGroupCatContType(lcusdocID);
                spdGW_Sum = ShippingDetail.GetTotalGWByDocID(lcusdocID);
                spdMEAS_Sum = ShippingDetail.GetTotalMEASByDocID(lcusdocID);
                spdVOL_Sum = ShippingDetail.GetTotalVOLByDocID(lcusdocID);
            }
            //txtFILENO.ReadOnly = true;
            BuildSPDList(strCondition, -1, nPage, out nNumOfPages, out dtList, null, null);
            rptShippingDetail.DataSource = dtList;
            rptShippingDetail.DataBind();
            LoadCusDoc();
        }


    }
    private void LoadCusDoc()
    {
        if (Request.QueryString["ID"] != null)
        {
            long cusdocID = long.Parse(Request.QueryString["ID"].ToString());
            CusdocModel obj = new CusdocModel();
            obj.ID = cusdocID;
            obj.Load(GV.cn, GV.trans);
            txtHBLNO.Text = obj.HBLNO;
            txtHBLSHPR.Text = obj.HBLSHPR.Replace("\n", "<br>");
            txtHBLCNEE.Text = obj.HBLCNEE.Replace("\n", "<br>");
            txtHBLNTF.Text = obj.HBLNTF.Replace("\n", "<br>");

            Control FooterTemplate = rptShippingDetail.Controls[rptShippingDetail.Controls.Count - 1].Controls[0];
            Control HeaderTemplate = rptShippingDetail.Controls[0].Controls[0];
            Label txtDOGMN = (Label)FooterTemplate.FindControl("txtDOGMN");
            txtDOGMN.Text = obj.DOGMN.Replace("\n", "<br>");
            Label ddTypeOfMovement = (Label)HeaderTemplate.FindControl("ddTypeOfMovement");
            ddTypeOfMovement.Text = obj.TypeOfMovement;
            //st2
            txtVSSL.Text = obj.VSSL;
            txtVOYNO.Text = obj.VOYNO;
            txtPORID.Text = obj.PORNAME.ToString();//Display PORNAME
            txtPOLID.Text = obj.POLNAME.ToString();//Display POLNAME
            txtSHONBOARD.Text = obj.SHONBOARD.ToString("MMM-dd-yyyy");
            if (obj.BACKDATE > DateTime.MinValue)
                txtSHONBOARD.Text = obj.BACKDATE.ToString("MMM-dd-yyyy");
            txtPODID.Text = obj.PODNAME.ToString();//Display PODNAME
            txtDESTID.Text = obj.DESTNAME.ToString();//Display DESTNAME

            //st3
            ddTOP.Text = obj.TOP;
            txtPAYABLEAT.Text = obj.PAYABLEATNAME;//Display Port Name
            txtPOISS.Text = obj.POISSNAME;//Display Port Name
            ddNOORI.Text = obj.NOORI;
            txtDOISS.Text = obj.DOISS.ToString("MMM-dd-yyyy"); ;
            txtAGDLVR.Text = obj.AGDLVR.Replace("\n", "<br>");
            txtCARRN.Text = obj.CARRN.Replace("\n", "<br>");

            //st4
            //txtMBLSHPR.Text = obj.MBLSHPR;
            //txtMBLCNEE.Text = obj.MBLCNEE;
            //txtMBLNTF.Text = obj.MBLNTF;
            //txtBOOKER.Text = obj.BOOKER;
            //txtCTRNO.Text = obj.CTRNO.ToString();
            //txtCTRVALID.Text = obj.CTRVALID.ToString("dd-MMMM-yyyy"); ;
            //txtCTRNAME.Text = obj.CTRNAME;

            //st5
            txtRATE.Text = obj.RATE.ToString("#.##");
            txtUNIT.Text = obj.UNIT;
            txtCURR.Text = obj.CURR;
            if (obj.TOP.ToLower() == "prepaid")
                txtPREPAID.Text = obj.DVALUE.ToString("#,#.##");
            if (obj.TOP.ToLower() == "collect")
                txtCOLLECT.Text = obj.DVALUE.ToString("#,#.##");
            //txtSTUFF.Text = obj.STUFF;
        }

    }
    private void BuildSPDList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("INDEX");
        dtList.Columns.Add("CONT");
        dtList.Columns.Add("SEAL");
        dtList.Columns.Add("ContType");
        dtList.Columns.Add("PKGS");
        dtList.Columns.Add("GW");
        dtList.Columns.Add("MEAS");
        dtList.Columns.Add("TEMP");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;

        string[] strValues = null;
        if (Request.QueryString["ID"] != null)
        {
            strCondition += " AND CusdocID=" + Request.QueryString["ID"].ToString();

            ShippingDetail.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    string ID = dr["ID"].ToString();
                    string strIndex = count.ToString();

                    string CONT = dr["CONT"].ToString();
                    string SEAL = dr["SEAL"].ToString();
                    string ContType = dr["ContType"].ToString();
                    string PKGS = dr["PKGS"].ToString();

                    string GW = "";
                    double dGW = double.Parse(dr["GW"].ToString());
                    if (dGW > 0)
                        GW = dGW.ToString("#,#.00", CultureInfo.InvariantCulture);

                    string MEAS = "";
                    double dMEAS = double.Parse(dr["MEAS"].ToString());
                    if (dMEAS > 0)
                        MEAS = dMEAS.ToString("#,#.00", CultureInfo.InvariantCulture);


                    string TEMP = dr["TEMP"].ToString();
                    string Action = " <a href=\"javascript:doDeleteSPR('" + Request.QueryString["ID"].ToString() + "','" + dr["ID"].ToString() + "')\">DEL</a>";

                    strValues = new string[] { ID, strIndex, CONT, SEAL, ContType, PKGS, GW, MEAS, TEMP, Action };
                    dtList.Rows.Add(strValues);
                }
            }
        }

    }
}